﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace weighing
{
    /// <summary>
    /// OutPrint.xaml 的交互逻辑
    /// </summary>
    public partial class OutPrint : System.Windows.Controls.UserControl
    {

        private WeighingBillVo mWeighingBillVo;

        public OutPrint()
        {
            InitializeComponent();
            if(System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            this.Loaded += OutPrint_Loaded;
            this.DataContextChanged += OutPrint_DataContextChanged;

            HideCalculatePanel();

            ShowTime();
        }

        private void OutPrint_Loaded(object sender, RoutedEventArgs e)
        {
            this.printDateTimeTb.Content = CommonFunction.GetPrintTimeStr(mWeighingBillVo);

            SetBillDesc();
        }

        private void OutPrint_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext is WeighingBillVo bill)
            {
                mWeighingBillVo = bill;
                GeneraterQrCode();

                if (bill.Status == (int)BillStatus.Finished)
                {
                    this.WeigherTb.Text = bill.OutUserName;
                }
                else
                {
                    this.WeigherTb.Text = bill.InUserName;
                }
            }
        }


        private void SetBillDesc()
        {
            this.billHeaderTb.Text = CommonFunction.GetBillTitle(mWeighingBillVo);
            var def = " 白联存根，红联财务，黄联运费，绿联随车，其它";
            var desc = App.Config.BillPrintConfig.OutDesc;
            this.descTb.Text = desc.IsNullOrEmpty() ? def : desc;

        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        private void GeneraterQrCode()
        {
            //是否开启对像存储 
            switch (App.Config.ImgConfig.OosType)
            {
                case OosType.Local:
                    CommonFunction.ShowBillNumberQrCode(this.codeImage, mWeighingBillVo.Number);
                    break;
            
                default:
                    CommonFunction.ShowBillNumberQrCode(this.codeImage, mWeighingBillVo.Number);
                    break;
            }
        }


        /// <summary>
        /// 隐藏结算区域信息
        /// </summary>
        private void HideCalculatePanel()
        {
            if (App.Config.BillPrintConfig.OutShowCalculate == false)
            {
                this.PriceTbLabel.Visibility = Visibility.Collapsed;
                this.PriceTb.Visibility = Visibility.Collapsed;
                this.TotalMoneyTbLabel.Visibility = Visibility.Collapsed;
                this.TotalMoneyTb.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.PriceTbLabel.Visibility = Visibility.Visible;
                this.PriceTb.Visibility = Visibility.Visible;
                this.TotalMoneyTbLabel.Visibility = Visibility.Visible;
                this.TotalMoneyTb.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// 显示过磅时间
        /// </summary>
        private void ShowTime()
        {
            if (App.Config.WeighingConfig.OutTimes == 2)
            {
                this.InTimePanel.Visibility = Visibility.Visible;
                this.OutTimePanel.Visibility = Visibility.Visible;
                this.NetWeighingTimePanel.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.InTimePanel.Visibility = Visibility.Collapsed;
                this.OutTimePanel.Visibility = Visibility.Collapsed;
                this.NetWeighingTimePanel.Visibility = Visibility.Visible;
            }
        }

    }
}
